-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Allow weak/selectany external definitions in header units. #162713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang-modules @llvm/pr-subscribers-clang Author: None (akrieger) Changesweak and selectany are mechanisms for allowing the linker to resolve ODR violations. [module.import/6] states > A header unit shall not contain a definition of a non-inline function or variable whose name has external linkage. But this prevents compiling any headers with such weak symbols defined. These occur in eg. some Windows SDK headers like
Proposed solution: Do not emit Full diff: https://github.com/llvm/llvm-project/pull/162713.diff 2 Files Affected:
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 6eaf7b9435491..0063bd51aa612 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -13822,7 +13822,8 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, bool DirectInit) {
!VDecl->isInvalidDecl() && VDecl->isThisDeclarationADefinition() &&
VDecl->getFormalLinkage() == Linkage::External && !VDecl->isInline() &&
!VDecl->isTemplated() && !isa<VarTemplateSpecializationDecl>(VDecl) &&
- !VDecl->getInstantiatedFromStaticDataMember()) {
+ !VDecl->getInstantiatedFromStaticDataMember() &&
+ !(VDecl->hasAttr<SelectAnyAttr>() || VDecl->hasAttr<WeakAttr>())) {
Diag(VDecl->getLocation(), diag::err_extern_def_in_header_unit);
VDecl->setInvalidDecl();
}
@@ -16162,7 +16163,8 @@ Decl *Sema::ActOnStartOfFunctionDef(Scope *FnBodyScope, Decl *D,
!FD->isInvalidDecl() && !FD->isInlined() &&
BodyKind != FnBodyKind::Delete && BodyKind != FnBodyKind::Default &&
FD->getFormalLinkage() == Linkage::External && !FD->isTemplated() &&
- !FD->isTemplateInstantiation()) {
+ !FD->isTemplateInstantiation() &&
+ !(FD->hasAttr<SelectAnyAttr>() || FD->hasAttr<WeakAttr>())) {
assert(FD->isThisDeclarationADefinition());
Diag(FD->getLocation(), diag::err_extern_def_in_header_unit);
FD->setInvalidDecl();
diff --git a/clang/test/CXX/module/module.import/p6.cpp b/clang/test/CXX/module/module.import/p6.cpp
index cb2d799e5b565..9e378a5fe7759 100644
--- a/clang/test/CXX/module/module.import/p6.cpp
+++ b/clang/test/CXX/module/module.import/p6.cpp
@@ -3,6 +3,9 @@
// RUN: %clang_cc1 -std=c++20 -x c++-header %t/bad-header-unit.h \
// RUN: -emit-header-unit -o %t/bad-header-unit.pcm -verify
+// RUN: %clang_cc1 -std=c++20 -x c++-header %t/bad-header-unit-declspec.h \
+// RUN: -emit-header-unit -o %t/bad-header-unit.pcm -verify \
+// RUN: -fdeclspec
//--- bad-header-unit.h
@@ -77,3 +80,13 @@ template <typename T> bool b() {
}
inline bool B = b<int>();
+
+__attribute__((weak)) int weak_fun_definition() { return 42; }
+
+__attribute__((weak)) int weak_var_definition = 42;
+
+//--- bad-header-unit-declspec.h
+
+/* The cases below should compile without diagnostics. */
+
+__declspec(selectany) int selectany_var_definition = 42; // expected-no-diagnostics
|
|
832e71b
to
9bc35dd
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks not bad. Let's add a comment for it.
9bc35dd
to
0134ee7
Compare
Added a comment, in the correct style I hope. |
clang/lib/Sema/SemaDecl.cpp
Outdated
// The intent of the above section seems to be to prevent ODR issues. | ||
// Weak symbols do the same but are not part of the c++ spec. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should cite the wording in the spec first and explain why we do here as you said in summary of the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spec is cited already at lines 13819-13820 which is what I am referencing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://eel.is/c++draft/module.import#6
I could import the exact phrasing A header unit shall not contain a definition of a non-inline function or variable whose name has external linkage.
I guess? I will iterate on it and see if I find something I like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel better to have "we choose to permit entities with weak and selectany attribute as they are common in headers in practice."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really they are the same thing. It's not that they are common, they just have approximately the same semantics as inline.
I don't like the wording of "The intent of the above section seems to be" because it makes it sound like we don't actually know what the compiler is doing. We know it's disallowing multiple definitions of entities that are not allowed to have multiple definitions, and weak/selectany are just another way to allow multiple definitions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"the above section" was meant to refer to the section of the c++ spec, I see how that's ambiguous here.
0134ee7
to
21cf212
Compare
Summary: weak and selectany are mechanisms for allowing the linker to resolve ODR violations. [module.import/6] states > A header unit shall not contain a definition of a non-inline function or variable whose name has external linkage. But this prevents compiling any headers with such weak symbols defined. These occur in eg. some Windows SDK headers like `DirectXMath.h`.
21cf212
to
d6a4502
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM now.
@akrieger Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Thank you everyone for the review. |
weak and selectany are mechanisms for allowing the linker to resolve ODR violations. [module.import/6] states
But this prevents compiling any headers with such weak symbols defined. These occur in eg. some Windows SDK headers like
DirectXMath.h
.Proposed solution: Do not emit
diag::err_extern_def_in_header_unit
if theFD
orVDecl
have eitherSelectAnyAttr
orWeakAttr
.